﻿using System;
using System.Collections.Generic;
using SubSonic.Schema;

namespace MvcForum.Net.Bussiness
{
    public class UsersRepo
    {

        public static List<User> All()
        {
            return MainDB.Instant.User_ListAll().ExecuteTypedList<User>();
        }


        public static List<User> List(string filter, string sortBy,string orderBy, int pageIndex,int pageSize,out int totalRows)
        {

            var _sp= MainDB.Instant.User_List(pageIndex, pageSize);
            var _data = _sp.ExecuteTypedList<User>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);

            return _data;

        }

        public static UserInfo LogIn(string username, string password, bool updateState)
        {

            var data = MainDB.Instant.LogIn(username, password, updateState).ExecuteTypedList<UserInfo>();
            var _userInfo = data.Count > 0 ? data[0] : null;

            if(_userInfo!=null)
            {
                _userInfo.Permissions=new AccessMask(_userInfo.AccessMask);
                
            }




          /*  using (IDataReader _dr = MainDB.Instant.LogIn(username, password, updateState).ExecuteReader())
            {
                if (_dr.Read())
                {
                    _userInfo = new UserInfo();
                    _userInfo.UserId = Convert.ToInt32(_dr["UserId"]);
                    _userInfo.Email = _dr["Email"].ToString();
                    _userInfo.UserName = _dr["UserName"].ToString();
                    _userInfo.RoleId = Convert.ToInt32(_dr["RoleId"]);
                    _userInfo.IsActived = Convert.ToBoolean(_dr["IsActived"]);
                    bool _isAdmin = Convert.ToBoolean(_dr["IsAdmin"]);
                    _userInfo.IsAdmin = _isAdmin;
                    _userInfo.IsModerator = _isAdmin ? true : false;
                    _userInfo.TimeZone = Convert.ToSingle(_dr["TimeZone"]);
                    if (!_isAdmin)
                    {

                        var _am = new AccessMask();
                        _am.LoadFromString(_dr["AccessMask"].ToString());

                        _userInfo.Permissions = _am;
                    }
                }


            }

            if (_userInfo != null && !_userInfo.IsAdmin)
            {
                using (IDataReader _dr = MainDB.Instant.ModeratorList(_userInfo.UserId).ExecuteReader())
                {

                    var _forums = new ArrayList();

                    while (_dr.Read())
                    {

                        if (!_forums.Contains(_dr["ForumId"]))
                            _forums.Add(_dr["ForumId"]);

                    }

                    _userInfo.IsModerator = _forums.Count > 0;

                    _userInfo.Moderates = _forums;

                }


            }

*/

            return _userInfo;

        }


        public static User Info(int userId)
        {
            return UserInfo(userId);
        }

        public static User UserInfo(int userId)
        {
            var _list = MainDB.Instant.User_Info(userId, null).ExecuteTypedList<User>();
            return _list.Count > 0 ? _list[0] : null;
        }

        public static User UserInfo(string userName)
        {
            var _list = MainDB.Instant.User_Info(0, userName).ExecuteTypedList<User>();
            return _list.Count > 0 ? _list[0] : null;
        }

        
       /* public static void UpdateActiveCode(string email, string activeCode)
        {
            MainDB.Instant.User_UpdateActiveCode(email, activeCode).Execute();

        }*/

        public static void ResetPwd(int userId, string newPwd)
        {
            MainDB.Instant.User_ResetPwd(userId, newPwd).Execute();

        }



        public static User UserInfo(string activeCode, string email)
        {
            var _list = MainDB.Instant.User_GetByActiveCode(activeCode, email).ExecuteTypedList<User>();
            return _list.Count > 0 ? _list[0] : null;
        }

        public static bool UserNameExists(string userName)
        {

            StoredProcedure _sp = MainDB.Instant.User_ExistsByUserName(userName);
            _sp.Execute();
            return Convert.ToBoolean(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);
        }

        public static bool ChangePwd(int userId, string oldPwd, string pwd)
        {

            StoredProcedure _sp = MainDB.Instant.User_ChangePassword(userId, oldPwd, pwd);
            _sp.Execute();
            return Convert.ToBoolean(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);
        }


        public static bool EmailExists(string email)
        {

            StoredProcedure _sp = MainDB.Instant.User_EmailExists(email);
            _sp.Execute();
            return Convert.ToBoolean(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);
            //return Convert.ToBoolean(_sp.Output[0]);
        }


        public static void Register(string username, string pwd, string email, double timeZone)
        {

            MainDB.Instant.Register(username, pwd, email, timeZone).Execute();
        }


        public static List<User> OnlineList()
        {
            return MainDB.Instant.User_OnlineList(ForumConfiguration.Instant.PastMinutes).ExecuteTypedList<User>();
        }


        public static bool SaveEmailPwd(int userId,string oldpwd,string pwd,string email)
        {
            var _sp = MainDB.Instant.User_SaveEmailPwd(userId,oldpwd,pwd,email);
            _sp.Execute();

            return Convert.ToBoolean(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);

        }

        public static void SaveSignature(int userId,string signature)
        {

            MainDB.Instant.User_SaveSignature(userId, signature).Execute();

        }


        public static void SaveProfile(User u)
        {
            MainDB.Instant.User_SaveProfile(u.UserId,u.FullName,u.Address,u.Sex,u.Birthday,u.Website,u.Blog,u.YahooId,u.SkypeId,u.MSN,u.AOL,u.Bio,u.Hobby).Execute();
        }

        /*public static void SaveContact(int userId, string fullname,string address,short sex,DateTime birthday,string website,string blog,string ym,string sk,string msn,string aol)
        {

            MainDB.Instant.User_SaveContactDetails(userId, fullname,address,sex,birthday,website,blog,ym,sk,msn,aol).Execute();

        }
*/
        public static  void SaveAvatar(int userId,string avatar)
        {
            MainDB.Instant.User_SaveAvatar(userId,avatar).Execute();
        }

        public static void ToggleLock(int id)
        {
            MainDB.Instant.User_ToggleLock(id).Execute();
        }

    }
}
